package com.pingao.mp.modular.sys.controller;


import com.auth0.jwt.JWT;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pingao.mp.modular.sys.entity.Menu;
import com.pingao.mp.utils.Constant;
import com.pingao.mp.utils.set.MapBuilder;
import com.pingao.mp.modular.sys.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 菜单表  前端控制器
 * </p>
 *
 * @author 闻品高
 * @since 2018-09-26
 */
@RestController
@RequestMapping("/sys/menu")
public class MenuController {

    /**
     * 获取登录用户对应的菜单
     */
    @Autowired
    private MenuService menuService;

    @GetMapping("/getMenu")
    public Object getMenu(HttpServletRequest request) {
        String token = request.getHeader("token");
        String s = JWT.decode(token).getAudience().get(0);//从token里获取userId
        Long userId = Long.valueOf(s);
        List<Menu> list = menuService.getLoginMenu(userId);
        Map<String, Object> map = new HashMap<>();
        map.put("rows", list);
        return map;
    }

    @GetMapping("/list")
    public Object list(@RequestParam(value = "search", required = false) String search) {
        Map<String, Object> map = new HashMap<>();
        map.put("rows", menuService.getList(search));
        return map;
    }

    @PostMapping("/add")
    public Object add(Menu menu) {
        Map<String, Object> map = new HashMap<>();
        menu.setUserId(1L);
        menu.setCreateTime(LocalDateTime.now());
        menu.setUpdateTime(LocalDateTime.now());
        if (!menuService.save(menu)) {
            map.put("status", false);
            map.put("msg", "保存菜单出错......");
            return map;
        }
        map.put("status", true);
        map.put("msg", "保存菜单成功");
        map.put("menu", menu);
        return map;
    }

    @GetMapping("/ztree")
    public Object ztree(@RequestParam(value = "id", required = false) Long id) {
        return menuService.getMenuTree(id);
    }

    @DeleteMapping("/delete")
    public Object delete(
            @RequestParam(value = "ids") String ids
    ) {
        String[] split = ids.split(",");
        List<String> list = Arrays.asList(split);
        MapBuilder<String, Object> m = new MapBuilder<>();
        //判断有无子菜单，有子菜单不允许删除
        int count = menuService.count(new QueryWrapper<Menu>().in(Menu.COL_PARENT_ID, ids).notIn(Menu.COL_ID, list));
        if (count > 0) {
            m.put(Constant.ApiStatus, false).put(Constant.ApiMsg, "所选菜单，有子菜单不允许删除").put("ids", ids);
        } else {
            for (String id : list) {
                menuService.deleteMenuById(Long.parseLong(id));
            }
            m.put(Constant.ApiStatus, true).put(Constant.ApiMsg, "删除成功").put("ids", ids);
        }
        return m.get();
    }


    @GetMapping("/get")
    public Object edit(@RequestParam("id") Long id) {
        MapBuilder<String, Object> m = new MapBuilder<>();
        m.put(Constant.ApiStatus, true);
        Menu menu = menuService.getById(id);
        if (menu == null) {
            m.put(Constant.ApiMsg, "该菜单不存在--this menu is not exits!!!").put(Constant.ApiStatus, false);
        } else {
            Menu parentMenu = menuService.getById(menu.getParentId());
            m.put("parentMenuName", parentMenu == null ? "" : parentMenu.getName());
            m.put(Constant.ApiData, menu);
        }
        return m.get();
    }

    @PostMapping("/edit")
    public Object edit(Menu menu) {
        if (menu.getParentId() == null) {
            menu.setParentId(0L);
        }
        Map<String, Object> map = new HashMap<>();
        menu.setUserId(1L);
        menu.setUpdateTime(LocalDateTime.now());
        if (!menuService.updateById(menu)) {
            map.put("status", false);
            map.put("msg", "保存菜单出错......");
            return map;
        }
        map.put("status", true);
        map.put("msg", "保存菜单成功");
        map.put("menu", menu);
        return map;
    }
}
